// xc_lshop/article/list/index.js
var actbillclass = require('../model/article_class')
var actbill = require('../model/article_contents')


Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    seachval:"",
    bpid:"0",
    spid:"0"
  },
  xcloaddata: function (options, config) {
    var that=this;
    this.getallnav()
    this.getpagelist(that, 1)
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      strwhere["status"] = 1;
      strwhere["bpid"] = this.data.bpid;
      strwhere["spid"] = this.data.spid;
      strwhere["seachval"] = this.data.seachval;
      that.setData({ modellist: [] });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
   
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        }
        else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
  },
  onCancel:function(){
    this.setData({ seachval: "" })    
    var that = this;
    this.getpagelist(that, 1)
  },
  onSearch: function (ev) {  
    this.setData({ seachval: ev.detail })    
    var that = this;
    this.getpagelist(that, 1)
  },
  secnavchange(e){
    var that=this;

    var id=e.currentTarget.dataset.id;
    var index=e.currentTarget.dataset.index;

    this.setData({
      sindex:index,
      spid:id
    })
     that.getpagelist(that,1)

  },
  changenav(e){
    var that=this;
    var name=e.detail.name;
    var index=e.detail.index;
    var update={};
    if(index!=0){
      var child=that.data.nav[index - 1].children
      update['bpid']=name;
      if(child.length>0){
        update['spid']=child[0]['id'];
      }else{
        update['spid']=0;
      }
    }else{
      update['spid']=0;
      update['bpid']=0;

    }
    
    this.setData(update)
    this.getpagelist(that, 1)
  },
  getallnav(){
    var that=this;
    actbillclass.apiall().then(item => {
      that.setData({ nav: item.obj.tree })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    that.getpagelist(that, -1)
  },

  
})